package com.jhx.controller.user;

import com.github.pagehelper.PageInfo;
import com.jhx.entity.Result;
import com.jhx.pojo.Member;
import com.jhx.pojo.Product;
import com.jhx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/product_list")
    public String productList(String name, Model model,
                              @RequestParam(value = "pageMum", defaultValue = "1") Integer pageMum,
                              @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize
    ){
        List<Product> productAll = productService.findProductAll(name,pageMum,pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(productAll);
        model.addAttribute("productList",productAll);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productName",name);
        return "product_list";
    }

    /**
     * 根据id查询产品信息
     */
    @RequestMapping("/get_product")
    @ResponseBody
    public Result<Product> getProductById(Integer id ,Model model){
        Result<Product> result = new Result<>();
        result.setMsg("请填写用户名");
        result.setSuccess(false);
        if (id!=null){
            Product productById = productService.findProductById(id);
            result.setMsg("查询成功");
            result.setSuccess(true);
            result.setData(productById);
            return result;
        }
        return result;
    }
    @RequestMapping("/delete_product")
    @ResponseBody
    private Result<String> deleteProductById(String ids){
        int num=0;
        Result<String> result = new Result();
        result.setMsg("删除失败");
        result.setSuccess(false);
        String[] strArray = ids.split(",");
        List<String> list = Arrays.asList(strArray);
        System.out.println(list);
        int i = productService.deleteProductById(list);

        if (i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }
        return result;
    }
}
